package com.vil.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vil.common.ServerResponse;
import com.vil.entity.Authority;
import com.vil.entity.Function;
import com.vil.mapper.FunctionMapper;
import com.vil.service.FunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class FunctionServiceImpl implements FunctionService {

    @Autowired
    private FunctionMapper mapper;

    @Override
    public List<Function> getMainFunctionList(Authority auth) {
        return mapper.getMainFunctionList(auth);
    }

    @Override
    public List<Function> getSubFunctionList(Function fun) {
        return mapper.getSubFunctionList(fun);
    }

    @Transactional
    @Override
    public ServerResponse insertSelective(Function record) {
        if (record==null){
            return ServerResponse.buildErrorMsg("参数不能为空");
        }
        int i = mapper.insertSelective(record);
        if (i>0){
            return ServerResponse.buildSuccessMsg("新增成功");
        }
        return ServerResponse.buildErrorMsg("新增失败");
    }

    @Transactional
    @Override
    public ServerResponse deleteByPrimaryKey(Long id) {
        if (id<1){
            return ServerResponse.buildErrorMsg("参数不合法");
        }
        int i = mapper.deleteByPrimaryKey(id);
        if (i>0){
            return ServerResponse.buildSuccessMsg("删除成功");
        }
        return ServerResponse.buildErrorMsg("删除失败");
    }

    @Transactional
    @Override
    public ServerResponse updateByPrimaryKeySelective(Function record) {
        if (record==null){
            return ServerResponse.buildErrorMsg("参数不能为空");
        }
        int i = mapper.updateByPrimaryKeySelective(record);
        if (1>0){
            ServerResponse.buildSuccessMsg("修改成功");
        }
        return ServerResponse.buildErrorMsg("修改失败");
    }


    @Override
    public ServerResponse getById(Long id) {
        if (id<1||id==null){
            ServerResponse.buildErrorMsg("参数不合法");
        }
        Function function = mapper.selectByPrimaryKey(id);
        return ServerResponse.buildSuccessData(function);
    }

    @Transactional
    @Override
    public ServerResponse deleteMany(Long[] ids) {
        if (ids==null){
            return ServerResponse.buildErrorMsg("数组不能为空");
        }
        int i = mapper.deleteMany(ids);
        if (i>0){
            return ServerResponse.buildSuccessMsg("删除成功");
        }
        return ServerResponse.buildErrorMsg("删除失败");
    }

    @Override
    public ServerResponse getListByManyConditions(Integer pageIndex, Integer pageSize, Function record) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Function> list = mapper.getListByManyConditions(record);
        PageInfo pageInfo = new PageInfo(list);
        if (pageInfo!=null){
            return ServerResponse.buildSuccessData(pageInfo);
        }
        return ServerResponse.buildErrorMsg("列表不存在");
    }

}
